package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.zretc.dao.AuditoriumsMapper;
import com.zretc.dao.FilmTypeMapper;
import com.zretc.dao.SalesMapper;
import com.zretc.entity.Auditoriums;
import com.zretc.entity.FilmType;
import com.zretc.entity.Sales;
import com.zretc.entity.Totals;
import com.zretc.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SalesServiceImpl implements SalesService {

    @Autowired
    private SalesMapper salesMapper;
    @Autowired
    private FilmTypeMapper filmTypeMapper;
    @Autowired
    private AuditoriumsMapper auditoriumsMapper;

//    pageNum  分页页码 int
//     *         pageSize 分页条数  int
//     *         salesId  销售统计编码   Integer
//     *         screeningId  场次编码  Integer
//     *         filmName    电影名  String
//     *         screeningStartTime1   时间段1 String
//     *         screeningStartTime2   时间段2 String
//     *         filmTypeName         电影类型 String
    @Override
    public PageInfo<Sales> getCompanyByPage(Map<String, String> param) {
        int pageNum =  Integer.parseInt(param.get("pageNum"));
        int pageSize =  Integer.parseInt(param.get("pageSize"));

        // 开始分页
        PageHelper.startPage(pageNum,pageSize);


        //Integer salesId = Integer.valueOf(param.get("salesId"));
       // Integer screeningId = Integer.valueOf(param.get("screeningId"));
        String filmName = param.get("filmName");
        String screeningStartTime1 = param.get("screeningStartTime1");
        String screeningStartTime2 = param.get("screeningStartTime2");
        String auditoriumsName = param.get("auditoriumsName");

        //处理参数
        Map<String,String> map =new HashMap<>();

        if (screeningStartTime1==""||screeningStartTime1==null) {
            map.put("screeningStartTime1",null);
        }else {
            map.put("screeningStartTime1",screeningStartTime1);

        }
        if (screeningStartTime2==""||screeningStartTime2==null) {
            map.put("screeningStartTime2",null);
        }else {
            map.put("screeningStartTime2",screeningStartTime2);

        }
        if (filmName==""||filmName==null) {
            map.put("filmName",null);
        }else {
            map.put("filmName","%"+filmName+"%");

        }
        if ("全部".equals(auditoriumsName)||auditoriumsName==null||"".equals(auditoriumsName)){
            map.put("auditoriumsName",null);
        }else {
            map.put("auditoriumsName",auditoriumsName);
        }

        //调取dao方法
        List<Sales> sales = salesMapper.get(map);
        // 包装到PageInfo对象
        PageInfo<Sales> pageInfo = new PageInfo<>(sales);
        return pageInfo;
    }

    @Override
    public List<Sales> getBytoday() {

        List<Sales> bytoday = salesMapper.getBytoday();
        return  bytoday;
    }

    @Override
    public List<Totals> getByYear() {
        List<Totals> byYear = salesMapper.getByYear();
        return byYear;
    }

    @Override
    public List<Totals> getByMouth(String years) {
        List<Totals> byMouth = salesMapper.getByMouth(years);
        return byMouth;
    }

    @Override
    public List<Totals> getByDay(String years, String months) {
        List<Totals> byDay = salesMapper.getByDay(years, months);
        return byDay;
    }

    @Override
    public Map<String, Object> get() {

        Map<String,Object> map=new HashMap<>();

        Totals all = salesMapper.getAll();
        Totals today = salesMapper.getToday();
        Totals mouth = salesMapper.getMouth();
        Totals lastMouth = salesMapper.getLastMouth();
        Totals allFilm = salesMapper.getAllFilm();
        Totals mouthFilm = salesMapper.getMouthFilm();
        DecimalFormat df=new DecimalFormat("0.00%");

        if (mouth==null){
            map.put("MouthTatalproportion","-100%");
            map.put("MouthTpriceproportion","-100%");
        }else if (lastMouth==null){
            map.put("MouthTatalproportion","100%");
            map.put("MouthTpriceproportion","100%");
        }else {
            Integer total = mouth.getTotal();
            Integer total1 = lastMouth.getTotal();
            double a=(total-total1)/total1;
            String MouthTatalproportion = df.format(a);

            Float tprice = mouth.getTprice();
            Float tprice1 = lastMouth.getTprice();
            double b=(tprice-tprice1)/tprice1;
            String MouthTpriceproportion = df.format(b);
            map.put("MouthTatalproportion",MouthTatalproportion);
            map.put("MouthTpriceproportion",MouthTpriceproportion);
        }



        if (all==null){
            map.put("AllTatal",0);
            map.put("AllTprice",0);
        }else {
            if (all.getTotal()==null){
                map.put("AllTatal",0);
            }else {
                map.put("AllTatal",all.getTotal());
            }
            if (all.getTprice()==null){
                map.put("AllTprice",0);
            }else {
                map.put("AllTprice",all.getTprice());
            }
        }



        if (today==null){
            map.put("TodayTatal",0);
            map.put("TodayTprice",0);
        }else {
            if (today.getTotal() == null) {
                map.put("TodayTatal", 0);
            } else {
                map.put("TodayTatal", today.getTotal());
            }
            if (today.getTprice() == null) {
                map.put("TodayTprice", 0);
            } else {
                map.put("TodayTprice", today.getTprice());
            }
        }
        map.put("allFilm",allFilm.getTotal());
        map.put("mouthFilm",mouthFilm.getTotal());
        return map;
    }

    @Override
    public List<Auditoriums> findAllAuditoriums(){
        List<Auditoriums> auditoriumsList = auditoriumsMapper.getAuditoriumsList(null);
        return auditoriumsList;
    }


}
